﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Carbon.Web.Forms.jQueryForms;

namespace Demo.Demos.ComplexDataStructures
{
    public class FriendWrapperViewModel
    {
        private PersonViewModel Person { get; set; }
        private PersonViewModel Friend { get; set; }

        public void RemoveFriend()
        {
            Person.Friends.Remove(Friend);
        }

        public void SelectFriend()
        {
            Friend.SelectCurrent();
        }

        public DropDownListItemCollection<PersonViewModel, PersonViewModel> FriendOrder
        {
            get
            {
                return new DropDownListItemCollection<PersonViewModel, PersonViewModel>(Person.Friends, i => i, i => i.DisplayName, Friend, false);
            }
            set
            {
                if (value.SelectedItem != Friend)
                {
                    int currentItemIndex = Person.Friends.IndexOf(Friend);
                    int insertIndex = Person.Friends.IndexOf(value.SelectedItem);

                    Person.Friends.RemoveAt(currentItemIndex);
                    Person.Friends.Insert(insertIndex, Friend);
                }
            }
        }

        public FriendWrapperViewModel(PersonViewModel person, PersonViewModel friend)
        {
            if (person == null) throw new NullReferenceException("person");
            if (friend == null) throw new NullReferenceException("friend");
            Person = person;
            Friend = friend;
        }
    }
}